<?php
/**
 * @Author: xuzhiyi
 * @Date:   2016-02-01 09:31:38
 * @Last Modified by:   xuzhiyi
 * @Last Modified time: 2016-02-25 17:24:16
 * ------------------------------------------------ *
 * 公共控制器 *
 * ------------------------------------------------ */

namespace Admin\Controller;
use \Think\Controller;

class CommonController extends Controller{

	/* 初始化方法
	 * ------------------------*/
	public function _initialize(){
		$GLOBALS['COMMON'] = D('Configs')->getConfigs();
		$this->assign('COMMON',$GLOBALS['COMMON']);
		$this->assign('admin',session('admin'));
		
		//判断是否登录
		if(empty(session('admin'))){
			if(empty(cookie('loginName')) || empty(cookie('loginPwd'))){
				$this->redirect('Public/login');
				exit;	
			}
			$manager = D('managers')->getManagerInfo();
			if($manager==false){
				$this->redirect('Public/login');
				exit;	
			}
			session('admin',$manager);
		}

		//判断是否锁定
		if(session('admin.isLock')==1){
			$this->redirect('Public/login');
			exit;
		}

		//获取当前控制器权限Id及父Id,判断权限
		//存在该操作才去判断是否有权限Index只提供登录控制
		if(method_exists($this,ACTION_NAME) && CONTROLLER_NAME!="Index"){
			$auth=M('auths')->field('authId,parentId,authName,authIco')->where('controller="'.CONTROLLER_NAME.'" and action="'.ACTION_NAME.'"')->find();
			$parentName=M('auths')->where('authId='.(int)$auth['parentId'])->getField('authName');
			$this->assign('authId',$auth['authId']);
			$this->assign('authName',$auth['authName']);
			$this->assign('parentId',$auth['parentId']);
			$this->assign('authIco',$auth['authIco']);
			$this->assign('parentName',$parentName);
			if(!in_array($auth['authId'],session('admin.grant')) && session('admin.loginName') !="admin"){
				
				$this->error('您无权访问该页面！','',3);
				
			}
		}
		//获取权限列表并在边栏显示
		if(session('admin.loginName')=="admin"){//超级管理员不受权限控制
			$auth = M('auths')->order('concat(authPath,authId)')->where('authFlag>0')->select();
		}else{
			$a=implode(session('admin.grant'),',');
			$auth = M('auths')->where("authId in($a) and authFlag>0")->order('concat(authPath,authId)')->select();
		}
		$this->assign('auth',$auth);

	}

	/* 空操作
	 * ------------------------*/
	public function _empty(){  
        header("HTTP/1.0 404 Not Found");//404状态码  
        $this->display("Public/404");//显示自定义的404页面模版  
    }

}